package com.example.androidmainshixun.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.androidmainshixun.R;
import com.example.androidmainshixun.entity.Moment;
import java.util.List;

public class MomentsAdapter extends RecyclerView.Adapter<MomentsAdapter.MomentViewHolder> {

    private List<Moment> momentList;

    public MomentsAdapter(List<Moment> momentList) {
        this.momentList = momentList;
    }

    @NonNull
    @Override
    public MomentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_moment, parent, false);
        return new MomentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MomentViewHolder holder, int position) {
        Moment moment = momentList.get(position);
        holder.username.setText(moment.getUsername());
        holder.postTime.setText(moment.getPostTime());
        holder.content.setText(moment.getContent());
        holder.postImage.setImageResource(moment.getImageResId()); // 绑定图片资源
    }

    @Override
    public int getItemCount() {
        return momentList.size();
    }

    class MomentViewHolder extends RecyclerView.ViewHolder {
        TextView username, postTime, content;
        ImageView postImage;

        public MomentViewHolder(@NonNull View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.tv_username);
            postTime = itemView.findViewById(R.id.tv_post_time);
            content = itemView.findViewById(R.id.tv_post_content);
            postImage = itemView.findViewById(R.id.iv_post_image); // 初始化图片视图
        }
    }
}